package l1j.server.server.command.executor;

import java.sql.Connection;
import java.sql.PreparedStatement;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.SQLUtil;

public class L1RepairCS implements L1CommandExecutor {
	private L1RepairCS() {
	}

	public static L1CommandExecutor getInstance() {
		return new L1RepairCS();
	}

	@Override
	public void execute(L1PcInstance pc, String cmdName, String arg) {
		try {
			deleteNewQueue();
			deleteChatingQueue();
			pc.sendPackets(new S_SystemMessage("客服系統已恢復。" ));
		}
		catch (Exception e) {
			pc.sendPackets(new S_SystemMessage("請輸入 : " + cmdName));
		}
	}
	
	public void deleteNewQueue(){
		Connection con = null;	
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("DELETE FROM cs_queue_list WHERE queuestatus='new' Order By datetime ASC limit 1");
			pstm.execute();
		} catch (Exception e) {
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void deleteChatingQueue(){
		Connection con = null;	
		PreparedStatement pstm = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("DELETE FROM cs_queue_list WHERE queuestatus='chating' Order By datetime ASC limit 1");
			pstm.execute();
		} catch (Exception e) {
		} finally {
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
}
